package com.bjpowernode.crm.service.impl;

import com.bjpowernode.crm.mapper.TransactionMapper;
import com.bjpowernode.crm.pojo.Page;
import com.bjpowernode.crm.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TranServiceImpl implements TranService {

    @Autowired
    private TransactionMapper transactionMapper;

    public void getPage(Page page, Map map) {
        // 查询总记录数
        Integer totalRows = transactionMapper.getCount(map);
        // 计算总页数
        Integer totalPages = (totalRows - 1) / page.getRowsPerPage() + 1;

        /*
            查询当前页的数据 limit startIndex,length
            limit 0,10  第1页
            limit 10,10  第2页
            limit 20,10  第3页
            ...
            limit (n-1)*10,10  第n页
         */
        Integer start = (page.getCurrentPage() - 1) * page.getRowsPerPage();
        Integer length = page.getRowsPerPage();
        List data = transactionMapper.getData(start, length,map);

        // 把数据添加到page对象中
        page.setTotalRows(totalRows);
        page.setTotalPages(totalPages);
        page.setData(data);
    }

    public List getCharts() {
        return transactionMapper.getCharts();
    }
}
